package org.linys.action.system;

import java.util.List;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.linys.action.BaseAction;
import org.linys.model.system.Operator;
import org.linys.service.common.CommonServ;
import org.linys.service.system.OperatorServ;
import org.linys.util.PageUtil;
import org.linys.util.ResponseMsgUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ModelDriven;

/**
 * 用户管理
 * 
 * @author LYS
 * 
 */
@SuppressWarnings({  "unchecked" })
@Controller("OperatorAction")
// 要与Struts.xml文件中class的属性相同
@Scope("prototype")
// 多例：每个请求一个action
public class OperatorAction extends BaseAction implements ModelDriven {

	private static final long serialVersionUID = 1L;
	private Operator operator = new Operator();

	private OperatorServ operatorServ;
	private CommonServ commonServ;
	private PageUtil pageUtil;
	private ResponseMsgUtil responseMsgUtil;
	
	

	public Object getModel() {
		return operator;
	}



	/**
	 * 退出
	 */
	public void exit() {
		request.getSession().removeAttribute("operator_session");//标注登录前，先注销。
		JSONObject object = new JSONObject();
		object.put("success", 1);
		this.responseMsgUtil.doResponse(object.toString());
	}

	
	/**
	 * 用户登录验证
	 */
	public void login() {
		Operator operate_session=(Operator) request.getSession().getAttribute("operator_session");
		//如果在线,直接返回主页面
		//注销本地的用户。
		if(operate_session!=null){
			synchronized(this){
				request.getSession().removeAttribute("operator_session");//标注登录前，先注销。
			}
		}
		// 根据用户名和密码判断是否允许登录
		Operator loginOperator = operatorServ.login(operator);
		JSONObject object = new JSONObject();
		if (null == loginOperator) {
			object.put("success", 0);
			object.put("msg", "用户名或密码不正确");
		}else{
			object.put("success", 1);
			request.getSession().setAttribute("operator_session", loginOperator);
			request.getSession().setAttribute("windowStyle", loginOperator.getWindowStyle());
		}
		this.responseMsgUtil.doResponse(object.toString());
	}
	
	/**
	 * 浏览用户列表
	 */
	public void view() {
		try{
		String rows = request.getParameter("rows");
		String page = request.getParameter("page");
		
		this.pageUtil.cal(operatorServ.count(operator), rows, page);
		List<Operator> operatorList = operatorServ.list(operator,this.pageUtil.getStart(), this.pageUtil.getEnd());

		JSONArray jsonArray = JSONArray.fromObject(operatorList);
		JSONObject object = new JSONObject();
		object.put("total", this.pageUtil.getTotal());
		object.put("rows", jsonArray);
		this.responseMsgUtil.doResponse(object.toString());
		}
		catch(Exception e){
			e.printStackTrace();
		}

	}

	/**
	 * 添加用户
	 */
	public void add() {
		JSONObject object = new JSONObject();
		try{
			Operator currentOperator=(Operator)request.getSession().getAttribute("operator_session");
			operator.setHandlerID(currentOperator.getOperatorID_());
			operator.setOperatorID(currentOperator.getOperatorID_());
			operator.setWindowStyle("v1");
			if (operatorServ.save(operator) > 0) {
				object.put("success", 1);
				object.put("operatorID_", operator.getOperatorID_());
			}
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			this.responseMsgUtil.doResponse(object.toString());
			
		}
	}

	/**
	 * 批量删除用户
	 */
	public void deleteByIDs() {
		String[] idsStr = operator.getIds().split(",");
		for (String id : idsStr) {
			try {
				operatorServ.deleteByID(new Integer(id));
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		JSONObject object = new JSONObject();
		object.put("success", 1);
		this.responseMsgUtil.doResponse(object.toString());
	}

	/**
	 * 修改用户
	 */
	public void update() {
		JSONObject object = new JSONObject();
		try{
			
			Operator currentOperator=(Operator)request.getSession().getAttribute("operator_session");
			operator.setOperatorID_(currentOperator.getOperatorID_());
			if (operatorServ.update(operator) > 0) {
				object.put("success", 1);
			}
		}
		catch(Exception e){
			e.printStackTrace();
		}finally{
			this.responseMsgUtil.doResponse(object.toString());
		}
	}
	
	/**
	 * 修改用户界面风格
	 */
	public void changeWindowStyle() {
		JSONObject object = new JSONObject();
		try{
			
			Operator currentOperator=(Operator)request.getSession().getAttribute("operator_session");
			operator.setOperatorID_(currentOperator.getOperatorID_());
			if (operatorServ.changeWindowStyle(operator) > 0) {
				request.getSession().setAttribute("windowStyle", operator.getWindowStyle());
				object.put("success", 1);
			}
		}
		catch(Exception e){
			e.printStackTrace();
		}finally{
			this.responseMsgUtil.doResponse(object.toString());
		}
	}
	
	/**
	 * 修改用户密码
	 */
	public void updatePassword() {
		JSONObject object = new JSONObject();
		try{
			
			Operator currentOperator=(Operator)request.getSession().getAttribute("operator_session");
			operator.setOperatorID_(currentOperator.getOperatorID_());
			if (operatorServ.updatePassword(operator) > 0) {
				object.put("success", 1);
			}
		}
		catch(Exception e){
			e.printStackTrace();
		}finally{
			this.responseMsgUtil.doResponse(object.toString());
		}
	}
	
	/**
	 * 验证修改密码时，是否原密码正确
	 */
	public void validatePassword() {
		Operator currentOperator=(Operator)request.getSession().getAttribute("operator_session");
		operator.setOperatorID_(currentOperator.getOperatorID_());
		if (operatorServ.validatePassword(operator) > 0) {
			this.responseMsgUtil.doResponse("true");
		}
	}

	
	public OperatorServ getOperatorServ() {
		return operatorServ;
	}

	@Autowired
	public void setOperatorServ(OperatorServ operatorServ) {
		this.operatorServ = operatorServ;
	}

	public CommonServ getCommonServ() {
		return commonServ;
	}

	@Autowired
	public void setCommonServ(CommonServ commonServ) {
		this.commonServ = commonServ;
	}

	@Autowired
	public void setPageUtil(PageUtil pageUtil) {
		this.pageUtil = pageUtil;
	}

	@Autowired
	public void setResponseMsgUtil(ResponseMsgUtil responseMsgUtil) {
		this.responseMsgUtil = responseMsgUtil;
	}

	
}
